//
//  WechatSetting.swift
//  LeGou
//
//  Created by lxm on 2022/8/3.
//

import UIKit

class WechatSetting: ZMViewController {

    //MARK:--------------------属性--------------------
    var currentImg = ""
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var wxText: UITextField!
    
    @IBOutlet weak var qqText: UITextField!
    
    @IBOutlet weak var imgButton: UIButton!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "微信号设置"
        let user = ZMAppShareData.share.safeUserInfo
        currentImg = user.wechatCode
        if currentImg.count > 0 {
            self.imgButton.zm.setImage(urlStr: currentImg, state: .normal)
        }
        wxText.text = user.wechatNo
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {

    }

    @IBAction func onSelectImg(_ sender: Any) {
        ZMSelectImage.show(1) { photos, assets in
            ZMToast.showActivity()
            AppHelper.uploadImgs(imgs: photos).subscribe(onNext:{ [unowned self] resp in
                if resp.code == .success {
                    ZMToast.hiddenActivity()
                    let img = resp.data ?? ""
                    self.currentImg = img
                    self.imgButton.zm.setImage(urlStr: img, state: .normal)
                } else {
                    ZMToast.showToast(title: resp.message)
                }
            }).disposed(by: self.disposeBag)
        }
    }
    
    @IBAction func onConfirm(_ sender: Any) {
        if wxText.text!.zm.isEmpty() {
            ZMToast.showToast(title: "请填写微信号")
            return
        }
        
//        if currentImg == "" {
//            ZMToast.showToast(title: "请上传微信二维码")
//            return
//        }
        ZMToast.showActivity()
        MineAPI.setWx(["wechatCode" : currentImg, "wechatNo" : wxText.text!]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.showToast(title: "保存成功")
            ZMAppShareData.share.requestUserInfo { ok in
                
            }
            self.navigationController?.popViewController(animated: true)
        }).disposed(by: disposeBag)
    }
    
    //MARK:--------------------delegate--------------------

}
